package sunsun.xiaoli.jiarebang.device.pondteam;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.itboye.pondteam.app.MyApplication;
import com.itboye.pondteam.base.BaseActivity;
import com.itboye.pondteam.utils.Const;
import com.itboye.pondteam.utils.SPUtils;
import com.itboye.pondteam.utils.languageutil.LocaleManager;
import com.itboye.pondteam.utils.loadingutil.MAlert;
import sunsun.xiaoli.jiarebang.R;
import sunsun.xiaoli.jiarebang.device.LoginActivity;

import java.util.HashMap;

public class ChooseLanguageActivity extends BaseActivity {


    ListView lv_language;
    HashMap<String, String> languageHash = new HashMap<>();
    //    String language[] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
    int selectPosition = 0;
    ImageView img_back;
    String selectLanguage = "";
    TextView txt_right;
    TextView txt_title;
    LangauageAdapter adapter;

    /*
    * Arabic, Egypt (ar_EG) -----------------------------阿拉伯语，埃及
    Arabic, Israel (ar_IL) -------------------------------阿拉伯语，以色列
    Bulgarian, Bulgaria (bg_BG) ---------------------保加利亚语，保加利亚
    Catalan, Spain (ca_ES) ---------------------------加泰隆语，西班牙
    Czech, Czech Republic (cs_CZ) -----------------捷克语，捷克共和国
    Danish, Denmark(da_DK) ------------------------丹麦语，丹麦
    German, Austria (de_AT) -------------------------德语，奥地利
    German, Switzerland (de_CH) -------------------德语，瑞士
    German, Germany (de_DE) ----------------------德语，德国
    German, Liechtenstein (de_LI) ------------------德语，列支敦士登的
    Greek, Greece (el_GR) ----------------------------希腊语，希腊
    English, Australia (en_AU) -------------------------英语，澳大利亚
    English, Canada (en_CA) --------------------------英语，加拿大
    English, Britain (en_GB) ----------------------------英语，英国
    English, Ireland (en_IE) -----------------------------英语，爱尔兰
    English, India (en_IN) --------------------------------英语，印度
    English, New Zealand (en_NZ) ---------------------英语，新西兰
    English, Singapore(en_SG) --------------------------英语，新加坡
    English, US (en_US) -----------------------------------英语，美国
    English, Zimbabwe (en_ZA) --------------------------英语，津巴布韦
    Spanish (es_ES) ----------------------------------------西班牙
    Spanish, US (es_US) -----------------------------------西班牙语，美国
    Finnish, Finland (fi_FI) ---------------------------------芬兰语，芬兰
    French, Belgium (fr_BE) -------------------------------法语，比利时
    French, Canada (fr_CA) -------------------------------法语，加拿大
    French, Switzerland (fr_CH) --------------------------法语，瑞士
    French, France (fr_FR) --------------------------------法语，法国
    Hebrew, Israel (he_IL) ---------------------------------希伯来语，以色列
    Hindi, India (hi_IN) -------------------------------------印地语，印度
    Croatian, Croatia (hr_HR) ----------------------------克罗地亚语，克罗地亚
    Hungarian, Hungary (hu_HU) ------------------------匈牙利语，匈牙利
    Indonesian, Indonesia (id_ID) ------------------------印尼语，印尼
    Italian, Switzerland (it_CH) ----------------------------意大利语，瑞士
    Italian, Italy (it_IT) ---------------------------------------意大利语，意大利
    Japanese (ja_JP) ----------------------------------------日语
    Korean (ko_KR) ------------------------------------------朝鲜语
    Lithuanian, Lithuania (lt_LT) --------------------------立陶宛语，立陶宛
    Latvian, Latvia (lv_LV) ---------------------------------拉托维亚语，拉托维亚
    Norwegian-Bokmol, Norway(nb_NO) ---------------挪威语，挪威
    Dutch, Belgium (nl_BE) --------------------------------荷兰语，比利时
    Dutch, Netherlands (nl_NL) ---------------------------荷兰语，荷兰
    Polish (pl_PL) -------------------------------------------波兰
    Portuguese, Brazil (pt_BR) ---------------------------葡萄牙语，巴西
    Portuguese, Portugal (pt_PT) ------------------------葡萄牙语，葡萄牙
    Romanian, Romania (ro_RO) ------------------------罗马尼亚语，罗马尼亚
    Russian (ru_RU) ----------------------------------------俄语
    Slovak, Slovakia (sk_SK) ------------------------------斯洛伐克语，斯洛伐克
    Slovenian, Slovenia (sl_SI) ---------------------------斯洛文尼亚语，斯洛文尼亚
    Serbian (sr_RS) ----------------------------------------塞尔维亚语
    Swedish, Sweden (sv_SE) ----------------------------瑞典语，瑞典
    Thai, Thailand (th_TH) --------------------------------泰语，泰国
    Tagalog, Philippines (tl_PH) --------------------------菲律宾语，菲律宾
    Turkish, Turkey (tr_TR) -------------------------------土耳其语，土耳其
    Ukrainian, Ukraine (uk_UA) --------------------------联合王国
    Vietnamese, Vietnam (vi_VN) -----------------------越南语，越南
    Chinese, PRC (zh_CN)--------------------------------中文，中国
    Chinese, Taiwan (zh_TW)-----------------------------中文，台湾*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_language);
        txt_right.setVisibility(View.VISIBLE);
        txt_right.setText(R.string.submit);
        txt_title.setText(getStringValue(R.string.select_language));
        initHash();
        selectLanguage = LocaleManager.getLanguage(this);
        if (isEmpty(selectLanguage)) {
            selectLanguage = languageHash.get("0").split(":")[1];
        }
        setAdapter();
    }

    private void initHash() {
        languageHash.put("0", "English:en-GB");//英国
        languageHash.put("1", "Finnish:fi-FI");//芬兰
        languageHash.put("2", "Hungarian:hu-HU");//匈牙利
        languageHash.put("3", "Italy:it-IT");//意大利
        languageHash.put("4", "Norway:nb-NO");//挪威
        languageHash.put("5", "Dutch:nl-NL");//荷兰
        languageHash.put("6", "Polish:pt-BR");//波兰
        languageHash.put("7", "Portuguese:pt-PT");//葡萄牙
        languageHash.put("8", "Romania:ro-RO");//罗马尼亚
        languageHash.put("9", "Russia:ru-RU");//俄罗斯
        languageHash.put("10", "Swedish:sv-SE");//瑞典
        languageHash.put("11", "French:fr-FR");//法国
        languageHash.put("12", "Danish:da-DK");//丹麦
        languageHash.put("13", "Greek:el-GR");//希腊
        languageHash.put("14", "Czech:cs-CZ");//捷克
        languageHash.put("15", "Spain:es-ES");//西班牙
        languageHash.put("16", "German:de-DE");//德国
    }

    private void setAdapter() {
        for (int i = 0; i < languageHash.size(); i++) {
            String s = languageHash.get("" + i).split(":")[1];
            if (selectLanguage.equalsIgnoreCase(s)) {
                selectPosition = i;
                break;
            }
        }
        if (adapter == null) {
            adapter = new LangauageAdapter();
            lv_language.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        lv_language.setOnItemClickListener((adapterView, view, position, l) -> {
            selectLanguage = languageHash.get("" + position).split(":")[1];
            setAdapter();
        });
    }

    @Override
    public void onClick(View view) {
        if (view == img_back) {
            finish();
        } else if (view == txt_right) {
            MAlert.alert(getStringValue(R.string.oper_success));
            new Handler().postDelayed(() -> {
                SPUtils.put(MyApplication.getInstance(), null, Const.LANGUAGE, selectLanguage);
                LocaleManager.setNewLocale(ChooseLanguageActivity.this,selectLanguage);
//                LocaleManager.setNewLocale(ChooseLanguageActivity.this,"it");
//                MyApplication.getInstance().initLanguage();
                Intent intent = new Intent(ChooseLanguageActivity.this, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                MyApplication.getInstance().exit();
//                MyApplication.getInstance().exit();
                finish();
            }, 2000);

        }
    }

    class LangauageAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return languageHash.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = View.inflate(ChooseLanguageActivity.this, R.layout.item_choose_language, null);
                viewHolder = new ViewHolder();
                viewHolder.item_iv_status = convertView.findViewById(R.id.item_iv_status);
                viewHolder.item_tv_country = convertView.findViewById(R.id.item_tv_country);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.item_tv_country.setText(languageHash.get("" + position).split(":")[0]);
            if (position == selectPosition) {
                viewHolder.item_iv_status.setVisibility(View.VISIBLE);
            } else {
                viewHolder.item_iv_status.setVisibility(View.GONE);
            }
            return convertView;
        }

        class ViewHolder {
            ImageView item_iv_status;
            TextView item_tv_country;
        }
    }
}
